/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.just.dusin.ooii.util;

import java.util.Date;
import org.just.dusin.ooii.database.DatabaseConnectionUtil;
import org.just.dusin.ooii.database.RealDeviceBean;
import org.just.dusin.ooii.database.RealDeviceDao;
import org.just.dusin.ooii.database.RealDeviceInfoBean;
import org.just.dusin.ooii.database.RealDeviceInfoDao;

/**
 *
 * @author Administrator
 */
public class DeviceData {   
    
    public static double getTotal(byte[] d){
        double r=0;
        r = d[15]*65536*256+ByteUtil.getUnsignedByteValue(d[14])*65536+ByteUtil.getUnsignedByteValue(d[13])*256+ByteUtil.getUnsignedByteValue(d[12])+ByteUtil.getUnsignedByteValue(d[11])/256.0+ByteUtil.getUnsignedByteValue(d[10])/65536.0;
        if(r<0)
            r=0;
        return r;
    }
    public static double getFlowSpeed(byte[] d){
        double r=0;
        int mutilFacotr=2;
        int powof2=0;
        powof2 = ByteUtil.getUnsignedByteValue(d[3])-23;
        r = d[2]*65536+ByteUtil.getUnsignedByteValue(d[1])*256+ByteUtil.getUnsignedByteValue(d[0]);
        
        if(powof2>0)
            r *=powOf2(powof2);
        else
            r /=powOf2(-powof2);
        if(r<0)
            r=0;
        return r;
    }
    
    public static double getBeltSpeed(byte[] d){
        double r=0;       
        int powof2=0;
        int b;
        if(ByteUtil.getUnsignedByteValue(d[83])>127)
            b=ByteUtil.getUnsignedByteValue(d[83])-256;
        else
            b = ByteUtil.getUnsignedByteValue(d[83]);
        powof2 = b-23;
        r = d[82]*65536+ByteUtil.getUnsignedByteValue(d[81])*256+ByteUtil.getUnsignedByteValue(d[80]);
        
        if(powof2>0)
            r *=powOf2(powof2);
        else
            r /=powOf2(-powof2);
        if(r<0)
            r=0;
        return r;
    }
    
    public static boolean isShellOpen(byte[] d){
        boolean r;
        if(d[90]==0)
            r = true;
        else
            r=false;
        return r;
    }
    
    private static int powOf2(int n){
        int r=1;
        if(n==0)
            return 1;
        else if(n==1){
            return 2;
        }
        else {
            r<<=n;
        }
        return r;
    }
    
    public static boolean isCheckValid(byte[] d){
        boolean r;
        byte c ;
        c=d[0];
        for(int i=1;i<91;i++)
            c+=d[i];
        if(c==d[91])
            r=true;
        else
            r= false;
        return r;
    }
    
    
}
